Object.defineProperty(exports, "__esModule", {
    value: true
});

exports.default = void 0;

var _typeof2 = require("@babel/runtime/helpers/typeof");

var _slicedToArray2 = require("@babel/runtime/helpers/slicedToArray");

var sq_sdk_version = "1.4.2 fix：getSystemInfoSync 报错处理";

console.log("当前sqsdk版本为：".concat(sq_sdk_version));

var sdkData = {
    payMode: "",
    extraData: "",
    fx_game_id: "217",
    zy_game_id: "900",
    c_game_id: "android.game.xddq_wxxyxtg",
    fx_c_game_id: "23238",
    android_os_type: "android.game.xddq_xyx",
    other_os_type: "com.xddq.wxxyx",
    app_ext: "",
    isShowSubscribeMessage: false,
    openid: "",
    unionid: "",
    uid: "",
    guid: "",
    session_key: "",
    wx_system_info: "{}",
    pay_version: "",
    system: "",
    iosPayImgUrl: "https://web.oiz611.com/generalimg1/faxing/xddq/clickpay.png",
    shareReferer: "haxyxfx_g1_zd_qddq",
    shareConfigList: [],
    subscribeMessageKey: "",
    defaultShareConfig: {
        imageUrl: "",
        title: ""
    },
    wx_param: "",
    activityAppid: "wxd58f6d5c8a0f265d",
    questionnaireAppid: "wxebadf544ddae62cb",
    // 问卷appid
    questionnaireYek: "TbsF:D!kVc%BofPvN3.x5zjp(u9ES~AX",
    questionnaireGiftId: "10325",
    actor_id: "",
    // 角色id
    sid: "",
    // 服id
    communityAppid: "wxfe2e18a5e38915a8",
    // 社区appid
    communityGameId: "913",
    communityFid: "201"
};

var apiUrl = {
    login: "https://apigameh5.shxnetwork.com/enter/37wxxyx/".concat(sdkData.fx_game_id, "?a=quickAppEnter&pt_type=1"),
    loginReport: "https://apigameh5.shxnetwork.com/index.php?c=api&a=default&appid=37wxxyx&cc=api&ca=game_login_report",
    shareReport: "https://apigameh5.shxnetwork.com/index.php?c=api&a=default&appid=37wxxyx&cc=api&ca=game_share_report",
    shareConfigUrl: "https://apigameh5.shxnetwork.com/index.php?c=api&a=query_platform_game_publish_config&appid=37wxxyx&key=share_config&game_id=".concat(sdkData.fx_game_id),
    zyCreateOrder: "https://apipay.shxnetwork.com/paygate.php?c=wxmp&a=create_order",
    goPay: "https://apipay.shxnetwork.com/paygate.php?c=wxmp&a=go_pay",
    payMode: "https://apipay.shxnetwork.com/paygate.php?c=wxmp&a=pmode",
    subscribeMessage: "https://apigameh5.shxnetwork.com/index.php?c=api&a=default&appid=37wxxyx&game_id=".concat(sdkData.fx_game_id, "&cc=api&ca=subscribeTempletCallback"),
    questionnaire: "https://apigameh5.shxnetwork.com/index.php?c=api&a=default&appid=37wxxyx&game_id=".concat(sdkData.fx_game_id, "&cc=api&ca=getPublicConf&conf=miniprogram_path"),
    questionnaireGift: "https://apigameh5.shxnetwork.com/index.php?c=api&a=default&app37id=37wxxyx&game_id=".concat(sdkData.fx_game_id, "&cc=api&ca=gift"),
    report: "https://datain.shxnetwork.com/api/v1/gn/sqw/app_event_analytics?apikey=b5a84f7090d75e9e7c30ab116f1558f0"
};

// 随机分享内容
var getRandomShare = function getRandomShare(imgArr) {
    if (imgArr.length > 0) {
        var randomIndex = Math.floor(Math.random() * imgArr.length);
        return imgArr[randomIndex];
    } else {
        return sdkData.defaultShareConfig;
    }
};

var initShare = function initShare() {
    // 显示当前页面的转发按钮
    wx.showShareMenu();
    var query = "referer=".concat(sdkData.shareReferer);
    wx.request({
        url: apiUrl.shareConfigUrl,
        success: function success(res) {
            console.log("sq share_config----\x3e", res.data);
            var resData = res.data;
            if (+resData.code === 1) {
                sdkData.shareConfigList = resData.data.share_config;
                var shareConfig = getRandomShare(sdkData.shareConfigList);
                wx.onShareAppMessage(function() {
                    return {
                        title: shareConfig.title,
                        imageUrl: shareConfig.imageUrl,
                        query: query
                    };
                });
            } else {
                wx.onShareAppMessage(function() {
                    return {
                        title: defaultShareConfig.title,
                        imageUrl: defaultShareConfig.imageUrl,
                        query: query
                    };
                });
            }
        },
        fail: function fail() {
            wx.onShareAppMessage(function() {
                return {
                    title: defaultShareConfig.title,
                    imageUrl: defaultShareConfig.imageUrl
                };
            });
        }
    });
};

var generateUUID = function generateUUID() {
    var d = new Date().getTime();
    var guid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == "x" ? r : r & 3 | 8).toString(16);
    });
    return guid;
};

var base64Encode = function base64Encode(str) {
    var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var out, i, len;
    var c1, c2, c3;
    len = str.length;
    i = 0;
    out = "";
    while (i < len) {
        c1 = str.charCodeAt(i++) & 255;
        if (i == len) {
            out += _keyStr.charAt(c1 >> 2);
            out += _keyStr.charAt((c1 & 3) << 4);
            out += "==";
            break;
        }
        c2 = str.charCodeAt(i++);
        if (i == len) {
            out += _keyStr.charAt(c1 >> 2);
            out += _keyStr.charAt((c1 & 3) << 4 | (c2 & 240) >> 4);
            out += _keyStr.charAt((c2 & 15) << 2);
            out += "=";
            break;
        }
        c3 = str.charCodeAt(i++);
        out += _keyStr.charAt(c1 >> 2);
        out += _keyStr.charAt((c1 & 3) << 4 | (c2 & 240) >> 4);
        out += _keyStr.charAt((c2 & 15) << 2 | (c3 & 192) >> 6);
        out += _keyStr.charAt(c3 & 63);
    }
    return out;
};

var base64Decode = function base64Decode(input) {
    var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var that = this;
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
    while (i < input.length) {
        enc1 = _keyStr.indexOf(input.charAt(i++));
        enc2 = _keyStr.indexOf(input.charAt(i++));
        enc3 = _keyStr.indexOf(input.charAt(i++));
        enc4 = _keyStr.indexOf(input.charAt(i++));
        chr1 = enc1 << 2 | enc2 >> 4;
        chr2 = (enc2 & 15) << 4 | enc3 >> 2;
        chr3 = (enc3 & 3) << 6 | enc4;
        output = output + String.fromCharCode(chr1);
        if (enc3 != 64) {
            output = output + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
        }
    }
    output = _utf8_decode(output);
    return output;
};

var _utf8_decode = function _utf8_decode(utftext) {
    var string = "";
    var i = 0;
    var c = 0;
    var c1 = 0;
    var c2 = 0;
    var c3 = 0;
    while (i < utftext.length) {
        c = utftext.charCodeAt(i);
        if (c < 128) {
            string += String.fromCharCode(c);
            i++;
        } else if (c > 191 && c < 224) {
            c2 = utftext.charCodeAt(i + 1);
            string += String.fromCharCode((c & 31) << 6 | c2 & 63);
            i += 2;
        } else {
            c2 = utftext.charCodeAt(i + 1);
            c3 = utftext.charCodeAt(i + 2);
            string += String.fromCharCode((c & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
            i += 3;
        }
    }
    return string;
};

// md5加密 相关
var md5 = function md5(str) {
    var hexcase = 0;
    /* hex output format. 0 - lowercase; 1 - uppercase        */    var chrsz = 8;
    /* bits per input character. 8 - ASCII; 16 - Unicode      */
    /*
   * These are the functions you'll usually want to call
   * They take string arguments and return either hex or base-64 encoded strings
   */    return binl2hex(core_md5(str2binl(str), str.length * chrsz));
    /*
   * Calculate the MD5 of an array of little-endian words, and a bit length
   */    function core_md5(x, len) {
        /* append padding */
        x[len >> 5] |= 128 << len % 32;
        x[(len + 64 >>> 9 << 4) + 14] = len;
        var a = 1732584193;
        var b = -271733879;
        var c = -1732584194;
        var d = 271733878;
        for (var i = 0; i < x.length; i += 16) {
            var olda = a;
            var oldb = b;
            var oldc = c;
            var oldd = d;
            a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936);
            d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
            c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
            b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
            a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897);
            d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
            c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
            b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
            a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
            d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
            c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
            b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
            a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
            d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
            c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
            b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);
            a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510);
            d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632);
            c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
            b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302);
            a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691);
            d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
            c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
            b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
            a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
            d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690);
            c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
            b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
            a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467);
            d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784);
            c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
            b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);
            a = md5_hh(a, b, c, d, x[i + 5], 4, -378558);
            d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
            c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
            b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
            a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060);
            d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
            c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
            b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
            a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
            d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222);
            c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
            b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
            a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487);
            d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
            c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
            b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);
            a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844);
            d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
            c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
            b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
            a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
            d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
            c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
            b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
            a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
            d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
            c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
            b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
            a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070);
            d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
            c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
            b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);
            a = safe_add(a, olda);
            b = safe_add(b, oldb);
            c = safe_add(c, oldc);
            d = safe_add(d, oldd);
        }
        return Array(a, b, c, d);
    }
    /*
   * These functions implement the four basic operations the algorithm uses.
   */    function md5_cmn(q, a, b, x, s, t) {
        return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
    }
    function md5_ff(a, b, c, d, x, s, t) {
        return md5_cmn(b & c | ~b & d, a, b, x, s, t);
    }
    function md5_gg(a, b, c, d, x, s, t) {
        return md5_cmn(b & d | c & ~d, a, b, x, s, t);
    }
    function md5_hh(a, b, c, d, x, s, t) {
        return md5_cmn(b ^ c ^ d, a, b, x, s, t);
    }
    function md5_ii(a, b, c, d, x, s, t) {
        return md5_cmn(c ^ (b | ~d), a, b, x, s, t);
    }
    /*
   * Add integers, wrapping at 2^32. This uses 16-bit operations internally
   * to work around bugs in some JS interpreters.
   */    function safe_add(x, y) {
        var lsw = (x & 65535) + (y & 65535);
        var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
        return msw << 16 | lsw & 65535;
    }
    /*
   * Bitwise rotate a 32-bit number to the left.
   */    function bit_rol(num, cnt) {
        return num << cnt | num >>> 32 - cnt;
    }
    /*
   * Convert a string to an array of little-endian words
   * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
   */    function str2binl(str) {
        var bin = Array();
        var mask = (1 << chrsz) - 1;
        for (var i = 0; i < str.length * chrsz; i += chrsz) bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << i % 32;
        return bin;
    }
    /*
   * Convert an array of little-endian words to a hex string.
   */    function binl2hex(binarray) {
        var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
        var str = "";
        for (var i = 0; i < binarray.length * 4; i++) {
            str += hex_tab.charAt(binarray[i >> 2] >> i % 4 * 8 + 4 & 15) + hex_tab.charAt(binarray[i >> 2] >> i % 4 * 8 & 15);
        }
        return str;
    }
};

// 获取系统信息
var getOStype = function getOStype() {
    var systemInfo = {};
    try {
        systemInfo = wx.getSystemInfoSync();
    } catch (error) {
        var errmsg = error instanceof Error ? "".concat(error.name, ", ").concat(error.message, ", ").concat(JSON.stringify(error.stack)) : JSON.stringify(error);
        report({
            event_parentid: 101,
            event_id: 10102,
            errmsg: "wx.getSystemInfoSync报错：".concat(errmsg)
        });
    }
    // js判断systemInfo是否对象
        if (Object.prototype.toString.call(systemInfo) !== "[object Object]") {
        report({
            event_parentid: 101,
            event_id: 10102,
            errmsg: "systemInfo内容异常：".concat(JSON.stringify(systemInfo))
        });
        systemInfo = {};
    }
    console.log("sq 系统信息-----\x3e", systemInfo);
    sdkData.wx_system_info = JSON.stringify(systemInfo);
    sdkData.system = systemInfo.system || "";
    createDeviceInfo(systemInfo);
};

var createDeviceInfo = function createDeviceInfo(res) {
    var UA = "";
    try {
        UA = encodeURIComponent(navigator.userAgent);
        console.log("had userAgent", UA);
    } catch (error) {
        console.log("no userAgent", error);
    }
    //判断res.system是字符串
        var DF = "", OS = "";
    if (res && res.system && typeof res.system === "string") {
        var _res$system$split = res.system.split(" ");
        var _res$system$split2 = _slicedToArray2(_res$system$split, 2);
        DF = _res$system$split2[0];
        OS = _res$system$split2[1];
        DF = DF || "";
        OS = OS || "";
    } else {
        report({
            event_parentid: 101,
            event_id: 10102,
            errmsg: "systemInfo.system是非字符串：".concat(JSON.stringify(res), "，").concat(JSON.stringify(UA))
        });
    }
    var deviceInfoObj = {
        IC: 2,
        DF: DF,
        OS: OS,
        AT: "",
        DC: "",
        PM: res && res.model || "",
        UA: UA,
        BW: "",
        RL: "",
        PN: "",
        PV: "",
        SDKV: res && res.SDKVersion || "",
        IDFA: "",
        ADID: "",
        UEADID: "",
        IMEI: "",
        OAID: "",
        LU: res && res.language || "",
        IDFV: "",
        CC: "",
        BT: "",
        DN: "",
        CI: "",
        MM: "",
        DISK: "",
        SFT: "",
        MODEL: "",
        TZ: "",
        CAID: "",
        CS: ""
    };
    var str = serializeDeviceInfoObj(deviceInfoObj);
    sdkData.device_info = base64Encode(str);
};

var serializeDeviceInfoObj = function serializeDeviceInfoObj(obj) {
    var str = [];
    for (var p in obj) {
        if (obj.hasOwnProperty(p)) {
            str.push(p + "=" + obj[p]);
        }
    }
    return str.join("|");
};

// 研发校验登录信息成功后，上报，给研发调用
var loginReport = function loginReport() {
    var data = {
        guid: sdkData.guid,
        game_id: sdkData.fx_game_id,
        uid: sdkData.uid,
        login_account: sdkData.openid,
        device_info: sdkData.device_info,
        app_ext: encodeURIComponent(sdkData.app_ext),
        c_game_id: sdkData.c_game_id
    };
    wx.request({
        url: apiUrl.loginReport,
        method: "post",
        header: {
            "content-type": "application/x-www-form-urlencoded"
        },
        data: data,
        success: function success(res3) {
            var resData = res3.data;
            if (+resData.code === 1) {
                console.log("登录上报成功=====>", resData);
            } else {
                console.log("登录上报失败=====>", resData);
            }
        },
        fail: function fail(err) {
            console.log("登录上报失败=====>", err);
        }
    });
    report({
        event_parentid: 140,
        event_id: 14001
    });
};

var loginErrorReport = function loginErrorReport() {
    report({
        event_parentid: 141,
        event_id: 14101
    });
};

var formatParams = function formatParams(data, noEncode) {
    var arr = [];
    for (var name in data) {
        var tmpname = name, tmpval = data[name];
        if (!noEncode) {
            tmpname = encodeURIComponent(name);
            tmpval = encodeURIComponent(data[name]);
        }
        arr.push(tmpname + "=" + tmpval);
    }
    return arr.join("&");
};

var shareReport = function shareReport() {
    var data = {
        uid: sdkData.uid,
        game_id: sdkData.fx_game_id,
        c_game_id: sdkData.c_game_id,
        sid: "",
        referer: sdkData.shareReferer,
        referer_param: "",
        ad_param: "",
        ad_type: "",
        bid: "",
        lid: "",
        ext: "",
        from_platform: "",
        tj_way: "",
        tj_from: "",
        device_info: sdkData.device_info,
        guid: sdkData.guid,
        app_ext: encodeURIComponent(sdkData.app_ext)
    };
    wx.request({
        url: apiUrl.shareReport,
        method: "post",
        header: {
            "content-type": "application/x-www-form-urlencoded"
        },
        data: data,
        success: function success(res) {
            var resData = res.data;
            if (+resData.code === 1) {
                console.log("sq 分享上报成功-----\x3e", resData);
            } else {
                console.log("sq 分享上报失败-----\x3e", resData);
            }
        },
        fail: function fail(err) {
            console.error("sq 分享上报请求失败-----\x3e", err);
        }
    });
};

var sendQuestionnaireGift = function sendQuestionnaireGift() {
    var giftTime = Math.floor(new Date().getTime() / 1e3);
    var sign = md5("appid=37wxxyx&unionid=".concat(sdkData.unionid, "&openid=").concat(sdkData.openid, "&gift_id=").concat(sdkData.questionnaireGiftId, "&server_id=").concat(sdkData.sid, "&actor_id=").concat(sdkData.actor_id, "&time=").concat(giftTime, "&key=").concat(sdkData.questionnaireYek));
    wx.request({
        url: apiUrl.questionnaireGift,
        method: "post",
        header: {
            "content-type": "application/x-www-form-urlencoded"
        },
        data: {
            openid: sdkData.openid,
            unionid: sdkData.unionid,
            gift_id: sdkData.questionnaireGiftId,
            server_id: sdkData.sid,
            actor_id: sdkData.actor_id,
            time: giftTime,
            sign: sign
        },
        success: function success(res) {
            console.log("sq 已通知发放问卷奖励-----\x3e", res);
        },
        fail: function fail(err) {
            console.error("sq 通知发放问卷奖励请求失败-----\x3e", err);
        }
    });
};

var initShow = function initShow() {
    var options = wx.getLaunchOptionsSync();
    console.log("sq 启动参数-----\x3e", options);
    if (options) {
        if (_typeof2(options.query) === "object" && options.scene) {
            options.query.scenenote = options.scene;
        }
        sdkData.wx_param = JSON.stringify(options.query);
        console.log("sq sdkData.wx_param-----\x3e", sdkData.wx_param);
        if (options.query && options.query.extraData) {
            sdkData.extraData = options.query.extraData;
        }
    }
};

var requestRetry = {
    count: 0,
    maxCount: 1
};

var report = function report(params) {
    try {
        console.log("event_parentid:", params.event_parentid);
        console.log("event_id:", params.event_id);
        var systemInfo = JSON.parse(sdkData.wx_system_info);
        var userdata = {
            SDKVersion: systemInfo.SDKVersion,
            deviceOrientation: systemInfo.deviceOrientation
        };
        // 米大师支付错误码
                if (params.midasPayCode) {
            userdata.midasPayCode = params.midasPayCode;
        }
        // 接口请求 非1 时服务端返回的code
                if (params.resultCode) {
            userdata.resultCode = params.resultCode;
        }
        // wx.login 返回的code
                if (params.jscode) {
            userdata.jscode = params.jscode;
        }
        // 设备性能等级（仅 Android）
                if (systemInfo.benchmarkLevel) {
            userdata.benchmarkLevel = systemInfo.benchmarkLevel;
        }
        //接口错误信息
                if (params.errmsg) {
            userdata.errmsg = params.errmsg;
        }
        var pack_info = {
            query: sdkData.wx_param
        };
        var data = {
            event_timestamp: Math.floor(new Date().getTime() / 1e3),
            guid: sdkData.guid,
            os: systemInfo.platform,
            // 游戏运行平台
            pid: "37wxxyx",
            // 37 平台appid
            app_type: "wechatgame",
            sdk_version: sq_sdk_version,
            // sdk的版本
            resolution: "".concat(systemInfo.screenWidth * systemInfo.pixelRatio, " * ").concat(systemInfo.screenHeight * systemInfo.pixelRatio),
            // 分辨率
            os_version: systemInfo.system,
            // 系统
            rom_version: systemInfo.version,
            // 微信版本号
            game_id: sdkData.fx_game_id,
            // 发行game_id
            cgame_id: sdkData.c_game_id,
            // 子游戏id
            phone_brand: systemInfo.brand,
            // 手机品牌
            phone_model: systemInfo.model,
            // 设备型号
            language: systemInfo.language,
            // 语言
            user_id: sdkData.uid,
            userdata: JSON.stringify(userdata),
            pack_info: base64Encode(JSON.stringify(pack_info)),
            event_parentid: params.event_parentid,
            event_id: params.event_id
        };
        var query = sdkData.wx_param ? JSON.parse(sdkData.wx_param) : {};
        if (query.referer) {
            data.referer = query.referer;
        }
        // cpu类型，安卓存在该字段
                if (systemInfo.cpuType) {
            data.cpu_model = systemInfo.cpuType;
        }
        // 内存大小，手机存在该字段
                if (systemInfo.memorySize) {
            data.memory = systemInfo.memorySize;
        }
        wx.request({
            url: apiUrl.report,
            data: data,
            method: "post",
            success: function success(res) {
                if (+res.data.code === 1) {
                    console.log("sq 质量上报成功-----\x3e", res);
                    requestRetry.count = 0;
                } else {
                    console.error("sq 质量上报失败-----\x3e", res, requestRetry.count);
                    if (requestRetry.count < requestRetry.maxCount) {
                        requestRetry.count++;
                        report(params);
                    } else {
                        requestRetry.count = 0;
                    }
                }
            },
            fail: function fail(err) {
                console.error("sq 质量上报失败-----\x3e", err);
                if (requestRetry.count < requestRetry.maxCount) {
                    requestRetry.count++;
                    report(params);
                } else {
                    requestRetry.count = 0;
                }
            }
        });
    } catch (error) {
        console.error("sq 质量上报出错了-----\x3e", error);
    }
};

var init = function init() {
    sdkData.guid = generateUUID();
    getOStype();
    initShow();
    initShare();
};

var SDK = {
    // 分享
    share: function share() {
        var shareData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        var shareQuery = "", shareQueryObj = {};
        if (shareData.extraData) {
            shareQueryObj.extraData = shareData.extraData;
        }
        shareQueryObj.referer = sdkData.shareReferer;
        shareQuery = formatParams(shareQueryObj);
        console.log("sq 分享携带参数-----\x3e", shareQuery);
        var shareConfig = getRandomShare(sdkData.shareConfigList);
        wx.shareAppMessage({
            title: shareData.title || shareConfig.title,
            imageUrl: shareData.imageUrl || shareConfig.imageUrl,
            query: shareQuery
        });
        shareReport();
        typeof shareData.successCb === "function" && shareData.successCb();
    },
    // 消息订阅
    subscribeMessage: function subscribeMessage() {
        if (!sdkData.app_ext) return;
        var appext = JSON.parse(base64Decode(sdkData.app_ext));
        // 如果消息模板ID长度为0 或者 游戏中已经弹出过消息订阅弹窗，则不再弹
                if (appext.disposable_templet === null || appext.disposable_templet !== undefined && appext.disposable_templet.length === 0 || sdkData.isShowSubscribeMessage) return;
        wx.requestSubscribeMessage({
            tmplIds: appext.disposable_templet,
            success: function success(res) {
                console.log("订阅成功", res);
                sdkData.isShowSubscribeMessage = true;
                wx.request({
                    url: apiUrl.subscribeMessage,
                    method: "post",
                    header: {
                        "content-type": "application/x-www-form-urlencoded"
                    },
                    data: {
                        data: JSON.stringify(res),
                        openid: sdkData.openid,
                        unionid: sdkData.unionid,
                        sign: md5("37wxxyx" + sdkData.fx_game_id + sdkData.subscribeMessageKey)
                    },
                    success: function success(res2) {
                        console.log("成功上报", res2);
                    },
                    fail: function fail(err2) {
                        console.error("失败上报", err2);
                    }
                });
            },
            fail: function fail(err) {
                console.error("订阅失败", err);
            }
        });
    },
    // 跳转活动
    navigateToActivity: function navigateToActivity(_ref) {
        var role_data = _ref.role_data, url = _ref.url;
        console.log("role_data", role_data);
        if (!url) throw Error("请传入活动URL！");
        if (!sdkData.app_ext) return;
        var appext = JSON.parse(base64Decode(sdkData.app_ext));
        var sign = url.indexOf("?") > -1 ? "&" : "?";
        var activityUrl = "".concat(url).concat(sign, "user_token=").concat(appext.app_pst);
        console.log("activityUrl", activityUrl);
        var encodeUrl = base64Encode(activityUrl);
        wx.navigateToMiniProgram({
            appId: sdkData.activityAppid,
            envVersion: "trial",
            extraData: {
                url: encodeUrl,
                role_data: role_data
            }
        });
    },
    // 社区
    navigateToCommunity: function navigateToCommunity() {
        var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, url = _ref2.url;
        if (!sdkData.app_ext) return;
        var appext = JSON.parse(base64Decode(sdkData.app_ext));
        var encodeUrl = url ? base64Encode(url) : "";
        wx.navigateToMiniProgram({
            appId: sdkData.communityAppid,
            extraData: {
                app_pst: appext.app_pst,
                game_id: sdkData.communityGameId,
                fid: sdkData.communityFid,
                url: encodeUrl
            }
        });
    },
    // 问卷
    navigateToQuestionnaire: function navigateToQuestionnaire(questionData) {
        if (!questionData) throw Error("请提供 sid 和 actor_id");
        if (questionData && !questionData.sid) throw Error("请提供 sid");
        if (questionData && !questionData.actor_id) throw Error("请提供 actor_id");
        sdkData.sid = questionData.sid;
        sdkData.actor_id = questionData.actor_id;
        wx.request({
            url: apiUrl.questionnaire,
            method: "post",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            data: {
                time: Math.floor(new Date().getTime() / 1e3),
                sid: questionData.sid
            },
            success: function success(res) {
                var resData = res.data;
                if (+resData.code === 1) {
                    wx.navigateToMiniProgram({
                        appId: sdkData.questionnaireAppid,
                        path: "".concat(resData.data, "&openid=").concat(sdkData.uid),
                        success: function success(res) {
                            console.log("跳转问卷成功-----\x3e", res);
                            clearTimeout(sdkData.questionTimer);
                            sdkData.questionTimer = setTimeout(function() {
                                sendQuestionnaireGift();
                            }, 1e3 * 10);
                        },
                        fail: function fail(err) {
                            console.log("跳转问卷失败-----\x3e", err);
                        }
                    });
                } else {
                    wx.showModal({
                        title: "温馨提示",
                        content: resData.msg,
                        showCancel: false
                    });
                }
            },
            fail: function fail(err) {
                console.error("sq 获取问卷请求失败=====>", err);
                wx.showModal({
                    title: "温馨提示",
                    content: "获取问卷失败，请联系客服",
                    showCancel: false
                });
            }
        });
    },
    // orderData 订单参数
    // payFnData 支付相关回调：
    // iosPayShowModalCb ios支付显示跳转客服弹窗时的回调， iosPaySuccess ios支付跳转客服成功回调，iosPayFail ios支付跳转客服失败回调，pcPayCb pc打开支付显示二维码回调
    pay: function pay() {
        var orderData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        var payFnData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
        console.log("sq 下单参数-----\x3e", orderData);
        report({
            event_parentid: 152,
            event_id: 15201
        });
        var isAgainPay = false;
        // 扣款失败是否需要继续扣款一次
                var appext = JSON.parse(base64Decode(sdkData.app_ext));
        // 自营下单
                var ptCreateOrder = function ptCreateOrder() {
            orderData.c_game_id = sdkData.c_game_id;
            orderData.fx_c_game_id = sdkData.fx_c_game_id;
            orderData.sdk_version = "";
            orderData.h5_tj_data = sdkData.device_info;
            orderData.openid = sdkData.openid;
            orderData.session_key = sdkData.session_key;
            orderData.wx_param = sdkData.wx_param;
            orderData.wx_system_info = sdkData.wx_system_info;
            orderData.app_ext = encodeURIComponent(sdkData.app_ext);
            orderData.app_pst = encodeURIComponent(appext.app_pst);
            report({
                event_parentid: 153,
                event_id: 15301
            });
            return new Promise(function(resolve) {
                wx.request({
                    url: apiUrl.zyCreateOrder,
                    method: "post",
                    header: {
                        "content-type": "application/x-www-form-urlencoded"
                    },
                    data: orderData,
                    success: function success(res) {
                        var resData = res.data;
                        if (+resData.code === 1) {
                            report({
                                event_parentid: 154,
                                event_id: 15401
                            });
                            resolve(resData);
                        } else if (+resData.code === 1001) {
                            report({
                                event_parentid: 154,
                                event_id: 15401
                            });
                            wx.showToast({
                                title: "支付成功",
                                icon: "none",
                                duration: 2e3
                            });
                        } else {
                            report({
                                event_parentid: 155,
                                event_id: 15505,
                                resultCode: resData.code,
                                errmsg: resData.msg
                            });
                            wx.showModal({
                                title: "温馨提示",
                                content: resData.msg,
                                showCancel: false
                            });
                        }
                    },
                    fail: function fail(err) {
                        console.error("sq 下单失败-----\x3e", err);
                        report({
                            event_parentid: 155,
                            event_id: 15505,
                            errmsg: JSON.stringify(err)
                        });
                        wx.showModal({
                            title: "温馨提示",
                            content: "下单失败",
                            showCancel: false
                        });
                    }
                });
            });
        };
        // 切客服支付
                var openKf = function openKf() {
            wx.showModal({
                title: "支付提示",
                content: "即将打开客服聊天界面，输入“cz”或者“充值”可以获取支付链接",
                showCancel: false,
                confirmText: "我知道了",
                success: function success(res) {
                    if (res.confirm) {
                        var payTimer = setTimeout(function() {
                            typeof payFnData.iosPaySuccess === "function" && payFnData.iosPaySuccess();
                        }, 15 * 1e3);
                        typeof payFnData.iosPayShowModalCb === "function" && payFnData.iosPayShowModalCb();
                        wx.openCustomerServiceConversation({
                            showMessageCard: true,
                            sendMessageImg: sdkData.iosPayImgUrl,
                            success: function success(res) {
                                console.log("sq 跳转客服成功-----\x3e", res);
                                typeof payFnData.iosPaySuccess === "function" && payFnData.iosPaySuccess();
                                clearTimeout(payTimer);
                            },
                            fail: function fail(err) {
                                console.log("sq 跳转客服失败-----\x3e", err);
                                typeof payFnData.iosPayFail === "function" && payFnData.iosPayFail();
                                clearTimeout(payTimer);
                            }
                        });
                    }
                }
            });
        };
        // 普通支付并发货，is_enough：1 余额支付 2 小程序支付 3 pc支付
                var goPay = function goPay(order_id, is_enough) {
            report({
                event_parentid: 171,
                event_id: 17101
            });
            return new Promise(function() {
                wx.request({
                    url: apiUrl.goPay,
                    data: {
                        order_id: order_id
                    },
                    success: function success(res) {
                        console.log("扣款发货状态======>", res);
                        var resData = res.data;
                        if (+resData.code === 1) {
                            isAgainPay = false;
                            report({
                                event_parentid: 172,
                                event_id: 17201
                            });
                            if (is_enough === 1) {
                                wx.showModal({
                                    title: "温馨提示",
                                    content: "已通过余额成功购买商品",
                                    showCancel: false
                                });
                            } else {
                                wx.showModal({
                                    title: "温馨提示",
                                    content: "商品购买成功",
                                    showCancel: false
                                });
                            }
                        } else if (+resData.code === -2) {
                            report({
                                event_parentid: 173,
                                event_id: 17301,
                                resultCode: resData.code,
                                errmsg: resData.msg
                            });
                            if (isAgainPay) {
                                wx.showToast({
                                    title: "商品购买失败，如已扣款，请及时联系客服反馈",
                                    icon: "none",
                                    duration: 5e3,
                                    mask: true
                                });
                                return;
                            }
                            isAgainPay = true;
                            goPay(order_id, is_enough);
                        } else {
                            report({
                                event_parentid: 173,
                                event_id: 17301,
                                resultCode: resData.code,
                                errmsg: resData.msg
                            });
                            wx.showModal({
                                title: "温馨提示",
                                content: resData.msg,
                                showCancel: false
                            });
                        }
                    },
                    fail: function fail(err) {
                        console.error("扣款发货接口失败======>", err);
                        report({
                            event_parentid: 173,
                            event_id: 17301,
                            errmsg: JSON.stringify(err)
                        });
                        wx.showModal({
                            title: "温馨提示",
                            content: "商品购买失败",
                            showCancel: false
                        });
                    }
                });
            });
        };
        function getPayMode() {
            return new Promise(function(resolve) {
                var c_game_id = /android|mac|windows/i.test(sdkData.system) ? sdkData.android_os_type : sdkData.other_os_type;
                wx.request({
                    url: apiUrl.payMode,
                    method: "post",
                    header: {
                        "content-type": "application/x-www-form-urlencoded"
                    },
                    data: {
                        game_id: sdkData.zy_game_id,
                        c_game_id: c_game_id,
                        user_id: sdkData.uid,
                        version: sdkData.pay_version
                    },
                    success: function success(res) {
                        var resData = res.data;
                        if (+resData.code === 1) {
                            sdkData.payMode = resData.data.mode;
                            if (+resData.data.pay_on === 1) {
                                resolve();
                            } else {
                                wx.showModal({
                                    content: "目前暂不支持充值，请您敬请期待哦。",
                                    showCancel: false
                                });
                            }
                        } else {
                            wx.showModal({
                                title: "温馨提示",
                                content: resData.msg,
                                showCancel: false
                            });
                        }
                    },
                    fail: function fail(err) {
                        console.error("sq 获取支付模式失败-----\x3e", err);
                        wx.showModal({
                            title: "温馨提示",
                            content: "获取支付模式失败",
                            showCancel: false
                        });
                    }
                });
            });
        }
        // 执行支付代码
                getPayMode().then(function() {
            if (+sdkData.payMode === 1) {
                orderData.terminal = "wechat_mgame";
                // 米大师支付
                                ptCreateOrder().then(function(res) {
                    console.log("sq 米大师订单创建参数-----\x3e", res);
                    if (+res.data.enough === 1) {
                        goPay(res.data.order_id, +res.data.enough);
                    } else {
                        report({
                            event_parentid: 160,
                            event_id: 16001
                        });
                        // 余额不足，先米大师支付，再扣款
                                                wx.requestMidasPayment({
                            buyQuantity: res.data.buyQuantity,
                            currencyType: res.data.currencyType,
                            env: res.data.env,
                            mode: res.data.mode,
                            offerId: res.data.offerId,
                            platform: res.data.platform,
                            zoneId: res.data.zoneId,
                            outTradeNo: res.data.order_id,
                            success: function success(result) {
                                console.log("sq 米大师支付成功-----\x3e", result);
                                report({
                                    event_parentid: 161,
                                    event_id: 16101
                                });
                                goPay(res.data.order_id, +res.data.enough);
                            },
                            fail: function fail(err) {
                                console.log("sq 米大师支付失败-----\x3e", err);
                                if (err.errCode === 1 || err.errCode === -2) {
                                    report({
                                        event_parentid: 163,
                                        event_id: 16301,
                                        midasPayCode: err.errCode
                                    });
                                } else {
                                    report({
                                        event_parentid: 162,
                                        event_id: 16203,
                                        midasPayCode: err.errCode,
                                        errmsg: JSON.stringify(err)
                                    });
                                }
                                if (err.errCode !== 1 && err.errCode !== -2 && !/mac/i.test(sdkData.system)) {
                                    wx.showModal({
                                        title: "支付结果",
                                        content: err.errMsg,
                                        showCancel: false
                                    });
                                }
                                if (err.errCode !== 1 && err.errCode !== -2 && /mac/i.test(sdkData.system)) {
                                    wx.showModal({
                                        title: "code: ".concat(err.errCode),
                                        content: "很抱歉，功能正在加急开发中，请您先用手机进行游戏充值",
                                        showCancel: false
                                    });
                                }
                            }
                        });
                    }
                });
            } else if (+sdkData.payMode === 2) {
                orderData.terminal = "wechat_pub";
                // 公众号支付
                                ptCreateOrder().then(function(res) {
                    console.log("sq 客服订单创建参数-----\x3e", res);
                    openKf();
                }).catch(function(err) {
                    console.error("sq ios支付失败-----\x3e", err);
                });
            }
        });
    },
    // 登录
    login: function login(callback) {
        if (!callback) throw Error("请提供回调函数！");
        console.log("sq 登录开始-----\x3e");
        report({
            event_parentid: 104,
            event_id: 10401
        });
        wx.login({
            success: function success(res1) {
                console.log("sq 微信 code success -----\x3e", res1.code);
                var jscode = res1.code;
                report({
                    event_parentid: 116,
                    event_id: 11604,
                    jscode: jscode
                });
                if (jscode) {
                    wx.request({
                        url: apiUrl.login,
                        method: "post",
                        header: {
                            "content-type": "application/x-www-form-urlencoded"
                        },
                        data: {
                            js_code: jscode,
                            wx_param: sdkData.wx_param,
                            wx_system_info: sdkData.wx_system_info,
                            c_game_id: sdkData.c_game_id,
                            device_info: sdkData.device_info,
                            guid: sdkData.guid
                        },
                        success: function success(res2) {
                            console.log("sq 登录返回信息-----\x3e", res2);
                            var resData = res2.data;
                            if (+resData.code === 1) {
                                console.log("sq 登录正常返回信息");
                                sdkData.uid = resData.data.uid;
                                sdkData.openid = resData.data.openid;
                                sdkData.session_key = resData.data.session_key;
                                sdkData.fx_game_id = resData.data.game_id;
                                sdkData.unionid = resData.data.unionid;
                                sdkData.app_ext = decodeURIComponent(resData.data.app_ext);
                                console.log("sq app_pst-----\x3e", JSON.parse(base64Decode(sdkData.app_ext)).app_pst);
                                report({
                                    event_parentid: 116,
                                    event_id: 11605
                                });
                                callback({
                                    code: 1,
                                    data: resData.data,
                                    loginReport: loginReport,
                                    loginErrorReport: loginErrorReport,
                                    extraData: sdkData.extraData
                                });
                            } else if (+resData.code === 10001) {
                                console.log("sq 登录异常返回-----\x3e，账号已注销");
                                report({
                                    event_parentid: 118,
                                    event_id: 11805,
                                    resultCode: 10001
                                });
                                // 账号已注销
                                                                wx.showModal({
                                    title: "温馨提示",
                                    content: resData.msg,
                                    showCancel: false
                                });
                            } else {
                                console.log("sq 登录异常返回信息");
                                report({
                                    event_parentid: 118,
                                    event_id: 11805,
                                    resultCode: resData.code,
                                    errmsg: resData.msg ? resData.msg : "sq 登录异常返回信息，无错误信息返回"
                                });
                                callback({
                                    code: 0,
                                    data: resData.data ? resData.data : "",
                                    msg: resData.msg ? resData.msg : ""
                                });
                            }
                        },
                        fail: function fail(err) {
                            console.error("sq 登录失败=======>", err);
                            report({
                                event_parentid: 118,
                                event_id: 11805,
                                errmsg: JSON.stringify(err)
                            });
                            callback({
                                code: 0,
                                error: err
                            });
                        }
                    });
                }
            },
            fail: function fail(err) {
                console.error("sq 微信获取 code fail-----\x3e", err);
                report({
                    event_parentid: 118,
                    event_id: 11805,
                    errmsg: JSON.stringify(err)
                });
            },
            complete: function complete(com) {
                console.log("sq 微信获取 code complete-----\x3e", com);
            }
        });
    }
};

init();

var _default = SDK;

exports.default = _default;